package org.code.mallspringboot.controller;

import org.code.mallspringboot.dto.PmsAttributeDto;
import org.code.mallspringboot.pojo.PmsProductAttributeCategory;
import org.code.mallspringboot.pojo.PmsProductCategoryAttributeRelation;
import org.code.mallspringboot.service.PmsProductAttributeCategoryService;
import org.code.mallspringboot.service.PmsProductCategoryAttributeRelationService;
import org.code.mallspringboot.vo.AttributeListVo;
import org.code.mallspringboot.vo.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/productAttribute")
/**
    产品属性
 */
public class ProductAttributeController {

    @Autowired
    private PmsProductAttributeCategoryService pmsProductAttributeCategoryService;

    @Autowired
    private PmsProductCategoryAttributeRelationService pmsProductCategoryAttributeRelationService;

    //属性筛选
    @GetMapping("/category/list/withAttr")
    public CommonResult withAttr(){
        List<PmsProductAttributeCategory> list = pmsProductAttributeCategoryService.selectAll();
        return CommonResult.success(list);
    }

    //进编辑回显属性筛选
    @GetMapping("/attrInfo/{productCategoryId}")
    public CommonResult attrInfo(@PathVariable int productCategoryId){
        List<PmsProductCategoryAttributeRelation> list = pmsProductAttributeCategoryService.selectByproductCategoryId(productCategoryId);
        return CommonResult.success(list);
    }

    //商品类型
    @GetMapping("/category/list")
    public CommonResult list(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                             @RequestParam(value = "pageSize",defaultValue = "5") int pageSize){

        List<PmsProductAttributeCategory> list = pmsProductAttributeCategoryService.selectPage(pageNum, pageSize);
        int total = pmsProductAttributeCategoryService.total();
        Map map = new HashMap<>();
        map.put("list",list);
        map.put("total",total);
        if (map != null){
            return CommonResult.success(map);
        }
        return CommonResult.failed("查询失败");
    }

    //商品类型添加
    @PostMapping("/category/create")
    public CommonResult create(String name){
        if (pmsProductAttributeCategoryService.add(name) > 0){
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    //商品类型删除
    @GetMapping("/category/delete/{id}")
    public CommonResult delete(@PathVariable int id){
        if (pmsProductAttributeCategoryService.delete(id) > 0){
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }

    //商品类型-修改
    @PostMapping("/category/update/{id}")
    public CommonResult update(@PathVariable int id,String name){
        if (pmsProductAttributeCategoryService.update(id, name) > 0){
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }

    //商品类型-属性列表
    @GetMapping("/list/{id}")
    public CommonResult list(@PathVariable int id,
                             @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                             @RequestParam(value = "pageSize",defaultValue = "5") int pageSize,
                             @RequestParam(value = "type",defaultValue = "0") int type){

        List<AttributeListVo> list = pmsProductAttributeCategoryService.selectAttributePage(id,type, pageNum, pageSize);
        int total = pmsProductAttributeCategoryService.total2(id,type);
        Map map = new HashMap();
        map.put("list" , list);
        map.put("total" , total);
        if (map.size() > 0){
            return CommonResult.success(map);
        }
        return CommonResult.failed("查询+分页失败");
    }

    //添加商品属性/参数
    @PostMapping("/create")
    public CommonResult create(@RequestBody PmsAttributeDto pmsAttributeDto){
        if (pmsProductAttributeCategoryService.add_attribute(pmsAttributeDto) > 0){
            int total2 = pmsProductAttributeCategoryService.total2(pmsAttributeDto.getProductAttributeCategoryId(), pmsAttributeDto.getType());
            PmsProductAttributeCategory pmsProductAttributeCategory = new PmsProductAttributeCategory();
            pmsProductAttributeCategory.setId(pmsAttributeDto.getProductAttributeCategoryId());
            if (pmsAttributeDto.getType() == 0){
                pmsProductAttributeCategory.setAttributeCount(total2);
            }else{
                pmsProductAttributeCategory.setParamCount(total2);
            }
            System.out.println(pmsProductAttributeCategory);
            if (pmsProductAttributeCategoryService.update_attribute(pmsProductAttributeCategory) > 0){
                System.out.println("商品类型数量修改成功");
            }
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    //修改商品属性/参数回显
    @GetMapping("/{id}")
    public CommonResult getProductAttr(@PathVariable int id){
        PmsAttributeDto pmsAttributeDto = pmsProductAttributeCategoryService.select_attributeById(id);
        if (pmsAttributeDto != null){
            return CommonResult.success(pmsAttributeDto);
        }
        return CommonResult.failed("回显失败");
    }

    //修改商品属性/参数
    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable int id,@RequestBody PmsAttributeDto pmsAttributeDto){
        pmsAttributeDto.setProductAttributeCategoryId(id);
        if (pmsProductAttributeCategoryService.update_attribute2(pmsAttributeDto) > 0){
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }


    //删除商品属性/参数
    @PostMapping("/delete")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult delete_attribute(int[] ids){
        boolean flag = false;
        try {
            flag = false;
            for (int id : ids) {
                Integer type = pmsProductAttributeCategoryService.select_attribute_type(id);
                Integer productAttributeCategoryId = pmsProductAttributeCategoryService.select_attribute_productAttributeCategoryId(id);
                if (pmsProductAttributeCategoryService.delete_attribute(id) > 0){
//                    for (int type : types) {
//                        for (int productAttributeCategoryId : productAttributeCategoryIds) {
                            if (type == 0){
                                //修改属性
                                if (pmsProductAttributeCategoryService.update_attribute_count(productAttributeCategoryId) > 0)System.out.println("修改属性成功");
                            }else{
                                //修改参数
                                if (pmsProductAttributeCategoryService.update_attribute_param_count(productAttributeCategoryId) > 0)System.out.println("修改参数成功");
                            }
//                        }//for-productAttributeCategoryId
//                    }//for-type
                    if (pmsProductCategoryAttributeRelationService.delete_attribute(id) < 0) System.out.println("筛选属性表删除成功");
                    flag = true;
                }
            }//for-ids
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }

        if (flag){
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }
}
